"use strict";

const DISPLACEMENT_THRESHOLD = 0.8;

let emotions = {}; // 存放待比较的表情
let expressions = [];  // 存放形态系数
let displacement = 9999;  // 用于后续判断是否更新表情，9999这个值不重要

// 程序入口：
function main() {
  // 初始化
  JEEFACETRANSFERAPI.init({
    canvasId: "canvas", // 画布
    NNCpath: "src/model/", // 模型路径
    callbackReady: function (errCode) {
      if (errCode) {
        console.log(
          "ERROR - cannot init JEEFACETRANSFERAPI. errCode =",
          errCode
        );
        errorCallback(errCode);
        return;
      }
      console.log("INFO : JEEFACETRANSFERAPI is ready !!!");
      successCallback();
    } // end callbackReady()
  });
} // end main()

function successCallback() {
  // 执行下一帧操作
  nextFrame();
}

function errorCallback(errorCode) {
  // 错误警告提示
  alert("Some error occured " + errorCode);
}

function nextFrame() {
  // 检测到人脸
  if (JEEFACETRANSFERAPI.is_detected()) {
    // 得到一个具有3个欧拉角的数组，描述头部旋转
    let rotation = JEEFACETRANSFERAPI.get_rotationStabilized();
    // 得到具有形态系数的数组
    expressions = JEEFACETRANSFERAPI.get_morphTargetInfluences();
    getMeanDisplacement(rotation);
    // 当displacement大于阈值则更新表情
    if (displacement > DISPLACEMENT_THRESHOLD) {
      let emotions = get_emotions(); // 获取表情序列
      let keyMax = getMaxEmotion(emotions); // 获取最符合的表情

      document.getElementById("emotion-anger").style.opacity = 0;
      document.getElementById("emotion-surprise").style.opacity = 0;
      document.getElementById("emotion-happy").style.opacity = 0;
      document.getElementById("emotion-fear").style.opacity = 0;
      document.getElementById("emotion-sad").style.opacity = 0;
      document.getElementById("emotion-none").style.opacity = 0;
      switch (keyMax) {
        case "anger":
          document.getElementById("emotion-anger").style.opacity = 1;
          document.getElementById("text").innerText = "愤怒";
          break;
        case "fear":
          document.getElementById("emotion-fear").style.opacity = 1;
          document.getElementById("text").innerText = "害怕";
          break;
        case "happy":
          document.getElementById("emotion-happy").style.opacity = 1;
          document.getElementById("text").innerText = "高兴";
          break;
        case "sad":
          document.getElementById("emotion-sad").style.opacity = 1;
          document.getElementById("text").innerText = "委屈";
          break;
        case "surprise":
          document.getElementById("emotion-surprise").style.opacity = 1;
          document.getElementById("text").innerText = "惊讶";
          break;

        default:
          document.getElementById("emotion-none").style.opacity = 1;
          document.getElementById("text").innerText = "面无表情";
          break;
      }
    }

    //**************************************************************************** */

    console.log("Detected");
  } else {
    console.log("Not Detected");
  }
  // 重新调用nextFrame
  requestAnimationFrame(nextFrame);
}


function getMeanDisplacement(rotation) {
  displacement = 0;
  let factors = rotation.concat(expressions);
  for (let index = 0; index < factors.length; index++) {
    const element = Math.abs(factors[index]);
    displacement += element;
  }
}


function getMaxEmotion(emotions) {
  let keyMax = "";
  let numberMax = -100;
  // 找最大值
  for (const key in emotions) {
    if (emotions[key] > numberMax) {
      // 记录键值及该键值下的特征值
      numberMax = emotions[key];
      keyMax = key;
    }
  }
  if (numberMax < 0.27) {
    return "none";
  }
  return keyMax;
}
